/**
 * 
 */
package com.nari.baseinfo.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.utils.IBuildingFloorEntity;
import com.nari.coreframe.dao.hibernate3.GenericDaoImpl;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.utils.StringUtils;
import com.nari.rescontroller.dao.IResourceAccessControlDao;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-1
 */
public class HeatRoomDaoImpl extends GenericDaoImpl<IHeatRoom, String> implements IHeatRoomDao {
	private IResourceAccessControlDao iResourceAccessControlDao;
	
	/**
	 * 分页查询热用户列表
	 * @param houseNo 门牌号(可选)
	 * @param roomOwner 户主姓名(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-26
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatRoom> queryHeatRoomPage(String houseNo, String roomOwner,
			String projectUID, String communityUID, PageCond pageCond) throws Exception {
		String listHql = "from IHeatRoom hr where 1=1";
		//门牌号
		if(houseNo!=null && !"".equals(houseNo.trim())) {
			listHql += " and lower(hr.houseNo) like '%"+houseNo.trim().toLowerCase()+"%'";
		}
		//户主姓名
		if(roomOwner!=null && !"".equals(roomOwner.trim())) {
			listHql += " and lower(hr.roomOwner) like '%"+roomOwner.trim().toLowerCase()+"%'";
		}
		//所属项目
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			listHql += " and hr.projectID='"+projectUID.trim()+"'";
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			listHql += " and hr.communityID='"+communityUID.trim()+"'";
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			listHql += " and hr.projectID in("+authorityRes+")";
		}
		//统计结果集个数
		String countHql = "select count(*) " + listHql;
		int total = ((Long)this.getCurrentSession().createQuery(countHql).uniqueResult()).intValue();
		//结果集排序
		listHql += " order by hr.floorIndex asc,hr.houseNo asc";
		//结果集分页
		Query listQuery = this.getCurrentSession().createQuery(listHql);
		listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatRoom> roomPage = new ResultWithPageCond<IHeatRoom>();
		roomPage.setList(listQuery.list());
		roomPage.setPageCond(pageCond);
		return roomPage;
	}
	
	/**
	 * 通过热用户所属关系(所属项目、所属楼栋、所属分摊域)查询热用户列表
	 * 若项目ID、楼栋ID、分摊域ID均不存在，则返回空列表
	 * @param projectUID 项目ID(可选)
	 * @param communityUID 所属小区(可选)
	 * @param shareAreaUID 分摊域ID(可选)
	 * @param buildingUID 楼栋ID(可选)
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-19
	 */
	public List<IHeatRoom> queryHeatRoomsByBelongs(String projectUID, String communityUID,
			String shareAreaUID, String buildingUID) throws Exception {
		return this.queryHeatRoomsByBelongs(projectUID, communityUID, shareAreaUID, buildingUID, null);
	}
	
	/**
	 * 通过热用户所属关系(所属项目、所属楼栋、所属分摊域)查询热用户列表
	 * 若项目ID、楼栋ID、分摊域ID均不存在，则返回空列表
	 * @param projectUID 项目ID(可选)
	 * @param communityUID 所属小区(可选)
	 * @param shareAreaUID 分摊域ID(可选)
	 * @param buildingUID 楼栋ID(可选)
	 * @param ifMeasureCharge 是否参与分摊(可选)
	 * 1:不参与		2:参与
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-4-30
	 */
	public List<IHeatRoom> queryHeatRoomsByBelongs(String projectUID, String communityUID,
			String shareAreaUID, String buildingUID, Integer ifMeasureCharge) throws Exception {
		if((projectUID!=null && !"".equals(projectUID.trim())) 
				|| (communityUID!=null && !"".equals(communityUID.trim())) 
				|| (shareAreaUID!=null && !"".equals(shareAreaUID.trim())) 
				|| (buildingUID!=null && !"".equals(buildingUID.trim()))) {
			String listHql = "from IHeatRoom hr where 1=1";
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listHql += " and hr.projectID='"+projectUID.trim()+"'";
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listHql += " and hr.communityID='"+communityUID.trim()+"'";
			}
			//所属分摊域
			if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
				listHql += " and hr.shareAreaID='"+shareAreaUID.trim()+"'";
			}
			//所属楼栋
			if(buildingUID!=null && !"".equals(buildingUID.trim())) {
				listHql += " and hr.buildingID='"+buildingUID.trim()+"'";
			}
			//是否参与分摊
			if(ifMeasureCharge != null) {
				listHql += " and hr.ifMeasureCharge=" + ifMeasureCharge;
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				listHql += " and hr.projectID in("+authorityRes+")";
			}
			//结果集排序
			listHql += " order by hr.floorIndex asc,hr.houseNo asc";
			return this.runQueryHSql(listHql);
		}
		return new ArrayList<IHeatRoom>();
	}
	
	/**
	 * 通过热用户ID列表查询热用户信息
	 * @param idList 热用户ID列表，若ID列表为空，则返回空
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public List<IHeatRoom> queryHeatRoomsByIDList(List<String> idList) throws Exception {
		if(idList!=null && idList.size()>0) {
			StringBuffer hsql = new StringBuffer("select distinct hr from IHeatRoom hr ");
			hsql.append("where hr.id in(");
			for(int index=0; index<idList.size(); index++) {
				hsql.append("'");
				hsql.append(idList.get(index));
				hsql.append("'");
				if(index < idList.size()-1) {
					hsql.append(",");
				}
			}
			hsql.append(") ");
			hsql.append("order by hr.floorIndex,hr.houseNo");
			return this.runQueryHSql(hsql.toString());
		}
		return null;
	}
	
	/**
	 * 通过热用户ID列表查询热用户ID-热用户门牌号映射表
	 * @param idList 热用户ID列表，若列表为空，则返回一个空映射表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-13
	 */
	public Map<String, String> queryIdNameMapByIDList(List<String> idList) throws Exception {
		Map<String, String> idNameMap = new HashMap<String, String>();
		if(idList!=null && idList.size()>0) {
			StringBuffer hsql = new StringBuffer("select distinct hr.id,hr.houseNo from IHeatRoom hr ");
			hsql.append("where hr.id in(");
			for(int index=0; index<idList.size(); index++) {
				hsql.append("'");
				hsql.append(idList.get(index));
				hsql.append("'");
				if(index < idList.size()-1) {
					hsql.append(",");
				}
			}
			hsql.append(")");
			List<Object> resultsList = this.runQueryHSqlObject(hsql.toString());
			//格式化查询结果集
			if(resultsList!=null && resultsList.size()>0) {
				for(Object result: resultsList) {
					Object[] arrayMap = (Object[]) result;
					idNameMap.put(arrayMap[0].toString(), arrayMap[1].toString());
				}
			}
		}
		return idNameMap;
	}
	
	/**
	 * 通过分摊域等信息分页查询热用户列表
	 * @param shareAreaUID 所属分摊域(必选)
	 * 说明：若分摊域ID不存在，则返回零结果集
	 * @param houseNo 热用户门牌号(可选)
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param pageCond
	 * @return
	 * @author Xiaopeng
	 * @date 2013-8-21
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatRoom> queryHeatRoomPageByAreaUID(String shareAreaUID, 
			String houseNo, String projectUID, String communityUID, String buildingUID, 
			PageCond pageCond) throws Exception {
		List<IHeatRoom> heatRoomList = new ArrayList<IHeatRoom>();
		int total = 0;
		//分摊域存在时，分页查询热用户列表
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			String listHql = "from IHeatRoom hr " +
					"where hr.shareAreaID='"+shareAreaUID.trim()+"'";
			//门牌号
			if(houseNo!=null && !"".equals(houseNo.trim())) {
				listHql += " and lower(hr.houseNo) like '%"+houseNo.trim().toLowerCase()+"%'";
			}
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listHql += " and hr.projectID='"+projectUID.trim()+"'";
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listHql += " and hr.communityID='"+communityUID.trim()+"'";
			}
			//所属楼栋
			if(buildingUID!=null && !"".equals(buildingUID.trim())) {
				listHql += " and hr.buildingID='"+buildingUID.trim()+"'";
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				listHql += " and hr.projectID in("+authorityRes+")";
			}
			//统计结果集个数
			String countHql = "select count(*) " + listHql;
			total = ((Long)this.getCurrentSession().createQuery(countHql).uniqueResult()).intValue();
			//结果集排序
			listHql += " order by hr.floorIndex asc,hr.houseNo asc";
			//结果集分页
			Query listQuery = this.getCurrentSession().createQuery(listHql);
			listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			heatRoomList = listQuery.list();
		}
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatRoom> roomPage = new ResultWithPageCond<IHeatRoom>();
		roomPage.setList(heatRoomList);
		roomPage.setPageCond(pageCond);
		return roomPage;
	}
	
	/**
	 * 查询不隶属于某个分摊域下的热用户列表
	 * @param shareAreaUID 所属分摊域(必选)
	 * 说明：若分摊域ID不存在，则返回零结果集
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param houseNo 热用户门牌号(可选)
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	@SuppressWarnings("unchecked")
	public ResultWithPageCond<IHeatRoom> queryRoomPageExceptAreaUID(String shareAreaUID, 
			String projectUID, String communityUID, String buildingUID,String houseNo, 
			PageCond pageCond) throws Exception {
		List<IHeatRoom> heatRoomList = new ArrayList<IHeatRoom>();
		int total = 0;
		//分摊域存在时，分页查询热用户列表
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			String listHql = "from IHeatRoom hr " +
					"where (hr.shareAreaID is null or hr.shareAreaID='')";
			//门牌号
			if(houseNo!=null && !"".equals(houseNo.trim())) {
				listHql += " and lower(hr.houseNo) like '%"+houseNo.trim().toLowerCase()+"%'";
			}
			//所属项目
			if(projectUID!=null && !"".equals(projectUID.trim())) {
				listHql += " and hr.projectID='"+projectUID.trim()+"'";
			}
			//所属小区
			if(communityUID!=null && !"".equals(communityUID.trim())) {
				listHql += " and hr.communityID='"+communityUID.trim()+"'";
			}
			//所属楼栋
			if(buildingUID!=null && !"".equals(buildingUID.trim())) {
				listHql += " and hr.buildingID='"+buildingUID.trim()+"'";
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				listHql += " and hr.projectID in("+authorityRes+")";
			}
			//统计结果集个数
			String countHql = "select count(*) " + listHql;
			total = ((Long)this.getCurrentSession().createQuery(countHql).uniqueResult()).intValue();
			//结果集排序
			listHql += " order by hr.floorIndex asc,hr.houseNo asc";
			//结果集分页
			Query listQuery = this.getCurrentSession().createQuery(listHql);
			listQuery.setFirstResult(pageCond.getStart()).setMaxResults(pageCond.getLimit());
			heatRoomList = listQuery.list();
		}
		//构造返回类型
		pageCond.setTotal(total);
		ResultWithPageCond<IHeatRoom> roomPage = new ResultWithPageCond<IHeatRoom>();
		roomPage.setList(heatRoomList);
		roomPage.setPageCond(pageCond);
		return roomPage;
	}
	
	/**
	 * 批量设置热用户所属分摊域信息
	 * @param shareAreaUID 所属分摊域
	 * @param roomIDList 热用户ID列表，格式：XXX,XXX,XXX
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-3
	 */
	public ResponseMsg saveHeatRoomArea(String shareAreaUID, String roomIDList) throws Exception {
		//保存之前先把原来属于这个分摊域里面的用户删除，然后再重新保存
		String deleteHql = "update IHeatRoom hr " +
		      "set hr.shareAreaID=' ' " +
		         "where hr.shareAreaID='"+shareAreaUID+"'";
         this.runFormatHSql(deleteHql);
		if(roomIDList.length()>1){
			String formatRoomIDList = StringUtils.formatIDListWithSyntax(roomIDList, ",", "'");
			String updateHql = "update IHeatRoom hr " +
					"set hr.shareAreaID='"+shareAreaUID+"' " +
					"where hr.id in("+formatRoomIDList+")";
			this.runFormatHSql(updateHql);
		}
		
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 通过所属分摊域统计热用户数量
	 * @param shareAreaUID
	 * @param ifMeasureCharge
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public int countHeatRoomsByAreaUID(String shareAreaUID, 
			Integer ifMeasureCharge) throws Exception {
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			StringBuffer counterHql = new StringBuffer("select count(*) from IHeatRoom hr ");
			counterHql.append("where hr.shareAreaID='");
			counterHql.append(shareAreaUID);
			counterHql.append("'");
			if(ifMeasureCharge != null) {
				counterHql.append(" and hr.ifMeasureCharge=");
				counterHql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				counterHql.append(" and hr.projectID in("+authorityRes+")");
			}
			return ((Long)this.runQueryHSqlObject(counterHql.toString()).get(0)).intValue();
		}
		return 0;
	}
	
	/**
	 * 通过所属分摊域查询热用户列表
	 * @param shareAreaUID 所属分摊域(必填)
	 * @param ifMeasureCharge 是否参与计量收费
	 * 说明：1:不参与		2:参与
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public List<IHeatRoom> queryHeatRoomsByAreaUID(String shareAreaUID, 
			Integer ifMeasureCharge) throws Exception {
		List<IHeatRoom> roomList = new ArrayList<IHeatRoom>();
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			StringBuffer hsql = new StringBuffer("from IHeatRoom hr ");
			hsql.append("where hr.shareAreaID='");
			hsql.append(shareAreaUID);
			hsql.append("'");
			if(ifMeasureCharge != null) {
				hsql.append(" and hr.ifMeasureCharge=");
				hsql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				hsql.append(" and hr.projectID in("+authorityRes+")");
			}
			roomList = this.runQueryHSql(hsql.toString());
		}
		return roomList;
	}
	
	/**
	 * 通过所属分摊域统计热用户建筑面积之和
	 * @param shareAreaUID
	 * @param ifMeasureCharge
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public double sumRoomStructAreaByAreaUID(String shareAreaUID, 
			Integer ifMeasureCharge) throws Exception {
		double structAreaSum = 0;
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			StringBuffer counterHql = new StringBuffer("select sum(hr.structArea) ");
			counterHql.append("from IHeatRoom hr ");
			counterHql.append("where hr.shareAreaID='");
			counterHql.append(shareAreaUID);
			counterHql.append("'");
			if(ifMeasureCharge != null) {
				counterHql.append(" and hr.ifMeasureCharge=");
				counterHql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				counterHql.append(" and hr.projectID in("+authorityRes+")");
			}
			Object countObj = this.getCurrentSession().createQuery(counterHql.toString()).uniqueResult();
			if(countObj != null) {
				structAreaSum = (Double) countObj;
			}
		}
		return structAreaSum;
	}
	
	/**
	 * 通过所属分摊域统计热用户供热面积之和
	 * @param shareAreaUID
	 * @param ifMeasureCharge
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public double sumRoomHeatAreaByAreaUID(String shareAreaUID, 
			Integer ifMeasureCharge) throws Exception {
		double structAreaSum = 0;
		if(shareAreaUID!=null && !"".equals(shareAreaUID)) {
			StringBuffer counterHql = new StringBuffer("select sum(hr.heatArea) ");
			counterHql.append("from IHeatRoom hr ");
			counterHql.append("where hr.shareAreaID='");
			counterHql.append(shareAreaUID);
			counterHql.append("'");
			if(ifMeasureCharge != null) {
				counterHql.append(" and hr.ifMeasureCharge=");
				counterHql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				counterHql.append(" and hr.projectID in("+authorityRes+")");
			}
			Object countObj = this.getCurrentSession().createQuery(counterHql.toString()).uniqueResult();
			if(countObj != null) {
				structAreaSum = (Double) countObj;
			}
		}
		return structAreaSum;
	}
	
	/**
	 * 通过所属楼栋统计热用户建筑面积之和
	 * @param buildingUID
	 * @param ifMeasureCharge
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public double sumRoomStructAreaByBuildingUID(String buildingUID, 
			Integer ifMeasureCharge) throws Exception {
		double heatAreaSum = 0;
		if(buildingUID!=null && !"".equals(buildingUID)) {
			StringBuffer counterHql = new StringBuffer("select sum(hr.structArea) ");
			counterHql.append("from IHeatRoom hr ");
			counterHql.append("where hr.buildingID='");
			counterHql.append(buildingUID);
			counterHql.append("'");
			if(ifMeasureCharge != null) {
				counterHql.append(" and hr.ifMeasureCharge=");
				counterHql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				counterHql.append(" and hr.projectID in("+authorityRes+")");
			}
			Object countObj = this.getCurrentSession().createQuery(counterHql.toString()).uniqueResult();
			if(countObj != null) {
				heatAreaSum = (Double) countObj;
			}
		}
		return heatAreaSum;
	}
	
	/**
	 * 通过所属楼栋统计热用户供热面积之和
	 * @param buildingUID
	 * @param ifMeasureCharge
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-6
	 */
	public double sumRoomHeatAreaByBuildingUID(String buildingUID, 
			Integer ifMeasureCharge) throws Exception {
		double heatAreaSum = 0;
		if(buildingUID!=null && !"".equals(buildingUID)) {
			StringBuffer counterHql = new StringBuffer("select sum(hr.heatArea) ");
			counterHql.append("from IHeatRoom hr ");
			counterHql.append("where hr.buildingID='");
			counterHql.append(buildingUID);
			counterHql.append("'");
			if(ifMeasureCharge != null) {
				counterHql.append(" and hr.ifMeasureCharge=");
				counterHql.append(ifMeasureCharge);
			}
			//权限资源匹配
			String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
			if(!"".equals(authorityRes)) {
				counterHql.append(" and hr.projectID in("+authorityRes+")");
			}
			Object countObj = this.getCurrentSession().createQuery(counterHql.toString()).uniqueResult();
			if(countObj != null) {
				heatAreaSum = (Double) countObj;
			}
		}
		return heatAreaSum;
	}
	
	/**
	 * 根据热用户所属关系统计热用户的个数
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param shareAreaUID 所属分摊域(可选)
	 * @param ifMeasureCharge 是否参与计量收费(可选)
	 * @return
	 * @author Xiaopeng
	 * @date 2013-10-21
	 */
	public int countRoomSumByBelongs(String projectUID, String communityUID, String buildingUID, 
			String shareAreaUID, Integer ifMeasureCharge) throws Exception {
		Criteria countCriteria = this.getCurrentSession().createCriteria(IHeatRoom.class);
		//所属项目
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			countCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			countCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if(buildingUID!=null && !"".equals(buildingUID.trim())) {
			countCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属分摊域
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			countCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
		}
		//是否参与计量收费
		if(ifMeasureCharge != null) {
			countCriteria.add(Restrictions.eq("ifMeasureCharge", ifMeasureCharge));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			countCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		//统计结果集个数
		return ((Long)countCriteria.setProjection(Projections.rowCount()).list().get(0)).intValue();
	}
	
	/**
	 * 根据热用户所属关系统计热用户的建筑面积之和
	 * @param projectUID 所属项目(可选)
	 * @param communityUID 所属小区(可选)
	 * @param buildingUID 所属楼栋(可选)
	 * @param shareAreaUID 所属分摊域(可选)
	 * @param ifMeasureCharge 是否参与计量收费(可选)
	 * @return
	 * @author Xiaopeng
	 * @date 2013-10-21
	 */
	public double sumRoomStructAreaByBelongs(String projectUID, String communityUID, String buildingUID, 
			String shareAreaUID, Integer ifMeasureCharge) throws Exception {
		Criteria sumCriteria = this.getCurrentSession().createCriteria(IHeatRoom.class);
		//所属项目
		if(projectUID!=null && !"".equals(projectUID.trim())) {
			sumCriteria.add(Restrictions.eq("projectID", projectUID.trim()));
		}
		//所属小区
		if(communityUID!=null && !"".equals(communityUID.trim())) {
			sumCriteria.add(Restrictions.eq("communityID", communityUID.trim()));
		}
		//所属楼栋
		if(buildingUID!=null && !"".equals(buildingUID.trim())) {
			sumCriteria.add(Restrictions.eq("buildingID", buildingUID.trim()));
		}
		//所属分摊域
		if(shareAreaUID!=null && !"".equals(shareAreaUID.trim())) {
			sumCriteria.add(Restrictions.eq("shareAreaID", shareAreaUID.trim()));
		}
		//是否参与计量收费
		if(ifMeasureCharge != null) {
			sumCriteria.add(Restrictions.eq("ifMeasureCharge", ifMeasureCharge));
		}
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResSql();
		if(!"".equals(authorityRes)) {
			//这是一个直接与底层数据库耦合的查询，注意避免数模更改带来的系统问题
			sumCriteria.add(Restrictions.sqlRestriction("{alias}.t_project_id in("+authorityRes+")"));
		}
		Object sumObj = sumCriteria.setProjection(Projections.sum("structArea")).list().get(0);
		double structAreaSum = 0;
		if(sumObj != null) {
			structAreaSum = (Double) sumObj;
		}
		return structAreaSum;
	}
	
	/**
	 * 通过分摊域ID查询所属热用户ID列表
	 * @param shareAreaUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public String queryRoomUIDListByAreaUID(String shareAreaUID) throws Exception {
		String hsql = "select hr.id from IHeatRoom hr " +
				"where hr.shareAreaID='"+shareAreaUID+"'";
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			hsql += " and hr.projectID in("+authorityRes+")";
		}
		return this.runQueryHSqlObject(hsql).toString();
	}
	
	/**
	 * 通过分摊域ID查询所属热用户对应单元ID列表
	 * @param shareAreaUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public String queryUnitUIDListByAreaUID(String shareAreaUID) throws Exception {
		String hsql = "select distinct hr.unitID " +
				"from IHeatRoom hr " +
				"where hr.shareAreaID='"+shareAreaUID+"'";
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			hsql += " and hr.projectID in("+authorityRes+")";
		}
		return this.runQueryHSqlObject(hsql).toString();
	}
	
	/**
	 * 通过分摊域ID查询所属热用户对应楼栋ID列表
	 * @param shareAreaUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public String queryBuidlingUIDListByAreaUID(String shareAreaUID) throws Exception {
		String hsql = "select distinct hr.buildingID " +
				"from IHeatRoom hr " +
				"where hr.shareAreaID='"+shareAreaUID+"'";
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			hsql += " and hr.projectID in("+authorityRes+")";
		}
		return this.runQueryHSqlObject(hsql).toString();
	}
	
	/**
	 * 通过分摊域ID查询所属热用户对应楼层ID列表
	 * 楼层ID编码：楼栋ID-单元ID-楼层
	 * @param shareAreaUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public String queryFloorUIDListByAreaUID(String shareAreaUID) throws Exception {
		String hsql = "select hr.buildingID,hr.unitID,hr.floorIndex " +
				"from IHeatRoom hr " +
				"where hr.shareAreaID='"+shareAreaUID+"'";
		//权限资源匹配
		String authorityRes = iResourceAccessControlDao.queryAuthorityProjResHql();
		if(!"".equals(authorityRes)) {
			hsql += " and hr.projectID in("+authorityRes+")";
		}
		hsql += " group by hr.buildingID,hr.unitID,hr.floorIndex";
		List<Object> preCodeList = this.runQueryHSqlObject(hsql);
		//构造楼层ID列表
		StringBuffer floorUIDList = new StringBuffer("");
		if(preCodeList!=null && preCodeList.size()>0) {
			for(Object preCodeObj: preCodeList) {
				Object[] preCodeArray = (Object[])preCodeObj;
				String floorUID = preCodeArray[0].toString(); //楼栋ID
				floorUID += "-";
				floorUID += preCodeArray[1].toString(); //单元ID
				floorUID += "-";
				floorUID += preCodeArray[2].toString(); //楼层ID
				floorUIDList.append(floorUID);
				floorUIDList.append(",");
			}
		}
		if(floorUIDList.toString().endsWith(",")) {
			return floorUIDList.substring(0, floorUIDList.length()-1);
		}
		return floorUIDList.toString();
	}
	
	/**
	 * 通过单元ID查询楼层实体
	 * @param unitUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public List<IBuildingFloorEntity> queryFloorListByUnitUID(String unitUID) throws Exception {
		//构造楼层实体列表
		List<IBuildingFloorEntity> floorList = new ArrayList<IBuildingFloorEntity>();
		//查询单元所属楼栋ID
		String buildingUIDQuery = "select distinct bu.buildingID " +
				"from IBuildingUnit bu " +
				"where bu.id='"+unitUID+"'";
		List<Object> buildingUIDList = this.runQueryHSqlObject(buildingUIDQuery);
		if(buildingUIDList!=null && buildingUIDList.size()>0) {
			String buildingUID = (String) buildingUIDList.get(0);
			//单元中的楼层查询
			String floorQuery = "select distinct hr.floorIndex " +
					"from IHeatRoom hr " +
					"where hr.unitID='"+unitUID+"' " +
					"order by hr.floorIndex asc";
			List<Object> floorIndexList = this.runQueryHSqlObject(floorQuery);
			if(floorIndexList!=null && floorIndexList.size()>0) {
				for(Object floorIndex: floorIndexList) {
					//构造楼层实体
					String floorUID = buildingUID; //楼栋ID
					floorUID += "-";
					floorUID += unitUID; //单元ID
					floorUID += "-";
					floorUID += floorIndex.toString(); //楼层ID
					IBuildingFloorEntity floorEntity = new IBuildingFloorEntity();
					floorEntity.setId(floorUID);
					floorEntity.setFloorIndex(floorIndex.toString());
					//追加单元所属的楼层
					floorList.add(floorEntity);
				}
			}
		}
		return floorList;
	}
	
	/**
	 * 通过楼层ID查询所属的热用户列表
	 * 楼层ID编码：楼栋ID-单元ID-楼层
	 * @param floorUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public List<IHeatRoom> queryRoomListByFloorUID(String floorUID) throws Exception {
		List<IHeatRoom> roomList = new ArrayList<IHeatRoom>();
		if(floorUID!=null && !"".equals(floorUID)) {
			//楼层ID编码分解所属楼栋、单元、楼层
			String[] belongArray = floorUID.split("[-]");
			String buildingUID = belongArray[0];
			String unitUID = belongArray[1];
			String floorIndex = belongArray[2];
			//根据所属楼栋、单元、楼层查询热用户列表
			String hsql = "from IHeatRoom hr " +
					"where hr.buildingID='"+buildingUID+"' " +
					"and hr.unitID='"+unitUID+"' " +
					"and hr.floorIndex='"+floorIndex+"' " +
					"order by hr.houseNo asc";
			roomList = this.runQueryHSql(hsql);
		}
		return roomList;
	}
	
	/**
	 * 取消分摊域所属范围（项目、小区）内热用户的分摊域信息
	 * @param projectUID 分摊域所属项目
	 * @param communityUID 分摊域所属小区
	 * @param shareAreaUID
	 * @param belongAreaRoomIDList 隶属于该分摊域的热用户
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public void deleteAreaRoomArchitect(String projectUID, String communityUID, 
			String shareAreaUID, String belongAreaRoomIDList) throws Exception {
		String hsql = "update IHeatRoom hr " +
				"set hr.shareAreaID=null " +
				"where hr.projectID='"+projectUID+"' " +
				"and hr.communityID='"+communityUID+"' " +
				"and hr.shareAreaID='"+shareAreaUID+"'";
		if(belongAreaRoomIDList!=null && !"".equals(belongAreaRoomIDList)) {
			String formatRoomIDs = StringUtils.formatIDListWithSyntax(belongAreaRoomIDList, ",", "'");
			hsql += " and hr.id not in("+formatRoomIDs+")";
		}
		this.runFormatHSql(hsql);
	}
	
	/**
	 * 设置热用户所属的分摊域
	 * @param shareAreaUID
	 * @param belongAreaRoomIDList
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-12-20
	 */
	public void saveAreaRoomArchitect(String shareAreaUID, String belongAreaRoomIDList) throws Exception {
		if(belongAreaRoomIDList!=null && !"".equals(belongAreaRoomIDList)) {
			String formatRoomIDs = StringUtils.formatIDListWithSyntax(belongAreaRoomIDList, ",", "'");
			String hsql = "update IHeatRoom hr " +
					"set hr.shareAreaID='"+shareAreaUID+"' " +
					"where hr.id in("+formatRoomIDs+")";
			this.runFormatHSql(hsql);
		}
	}
	
	/**
	 * 查询热用户信息
	 * @param houseNo
	 * @param buildingUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-12
	 */
	public IHeatRoom queryRoomInfoByhouseNoBuildingUID(String houseNo, 
			String buildingUID) throws Exception {
		String hsql = "from IHeatRoom hr " +
				"where hr.houseNo='"+houseNo+"' " +
				"and hr.buildingID='"+buildingUID+"'";
		List<IHeatRoom> roomList = this.runQueryHSql(hsql);
		//用户信息
		IHeatRoom room = null;
		if(roomList!=null && roomList.size()>0) {
			room = roomList.get(0);
		}
		return room;
	}
	
	/**
	 * 根据热用户ID级联删除关联信息
	 * 包括：热用户、系统用户及角色、分配表、散热器、温度点、用户分摊账单
	 * 注意：热量表数据、分配表数据等表计数据具有不可恢复性，因此不能作为级联删除的对象
	 * @param roomUID
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-1-20
	 */
	public ResponseMsg deleteCascadeByRoomUID(String roomUID) throws Exception {
		//删除热用户所属散热器
		String delSinkerHql = "delete from PSinkInfo sink " +
				"where sink.roomId='"+roomUID+"'";
		this.runFormatHSql(delSinkerHql);
		//删除热用户所属分配表
		String delAssignMeterHql = "delete from IAssignMeter am " +
				"where am.roomId='"+roomUID+"'";
		this.runFormatHSql(delAssignMeterHql);
		//删除热用户所属的温度点
		String delTempPointHql = "delete from ITemperaturePoint tp " +
				"where tp.roomId='"+roomUID+"'";
		this.runFormatHSql(delTempPointHql);
		//删除热用户所属用户分摊账单
		String delSinkResultHql = "delete from IShareSinkResult ssr " +
				"where ssr.userShareResultID in(select usr.id " +
											"from IUserShareResult usr " +
											"where usr.roomId='"+roomUID+"')";
		this.runFormatHSql(delSinkResultHql);
		String delUserResultHql = "delete from IUserShareResult usr " +
				"where usr.roomId='"+roomUID+"'";
		this.runFormatHSql(delUserResultHql);
		//删除热用户所属系统用户、用户角色
		String delUserRoleHql = "delete from IUserRole ur " +
				"where ur.userUuid in(select u.id " +
									"from IUser u " +
									"where u.roomId='"+roomUID+"')";
		this.runFormatHSql(delUserRoleHql);
		String deleteUserHql = "delete from IUser u " +
				"where u.roomId='"+roomUID+"'";
		this.runFormatHSql(deleteUserHql);
		//删除热用户信息
		this.deleteEntityById(roomUID);
		return ResponseMsg.getInstance(true, "");
	}

	/**
	 * @return the iResourceAccessControlDao
	 */
	public IResourceAccessControlDao getiResourceAccessControlDao() {
		return iResourceAccessControlDao;
	}

	/**
	 * @param iResourceAccessControlDao the iResourceAccessControlDao to set
	 */
	public void setiResourceAccessControlDao(
			IResourceAccessControlDao iResourceAccessControlDao) {
		this.iResourceAccessControlDao = iResourceAccessControlDao;
	}
	
}
